package com.caigou.comm.base.network.interceptors

import android.text.TextUtils
import com.caigou.comm.base.utils.Logger
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author #Suyghur.
 * Created on 2021/09/13
 */
class MultiBaseUrlInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        // 获取原始的request
        val originalRequest = chain.request()
        // 获取原始的url
        val originalUrl = originalRequest.url
        // 获取原始的request的创建者builder
        val builder = originalRequest.newBuilder()
        // 获取原始的headers
        val headers = originalRequest.headers
        // 获取请求头里面设置的baseUrl
        val baseUrl = headers["baseUrl"]
        Logger.d("originalUrl: $originalUrl")
        Logger.d("baseUrl: $baseUrl")
        return if (!TextUtils.isEmpty(baseUrl)) {
            // 将其解析成httpUrl
            val baseHttpUrl = baseUrl?.toHttpUrlOrNull()
            // 重建新的HttpUrl，需要重新设置的url部分
            val newHttpUrl = originalUrl.newBuilder()
                .scheme(baseHttpUrl?.scheme!!)
                .host(baseHttpUrl.host)
                .port(baseHttpUrl.port)
                .build()
            // 获取处理后的新newRequest
            val newRequest = builder.url(newHttpUrl).build()
            Logger.d("newHttpUrl: ${newRequest.url}")
            chain.proceed(newRequest)
        } else {
            chain.proceed(originalRequest)
        }
    }
}